package com.example.mybatis_book.controller;


import com.example.mybatis_book.constant.Constant;
import com.example.mybatis_book.model.Result;
import com.example.mybatis_book.model.UserInfo;
import com.example.mybatis_book.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RequestMapping("/user")
@RestController
public class UserInfoController {

    @Autowired
    UserInfoService userService;

    @RequestMapping(value = "/login",produces = "application/json")
    public Result login(String userName, String password, HttpSession session) {
        log.info(userName+password);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return Result.fail("账号或密码为空");
        }
        UserInfo userInfo = userService.getUserByName(userName);
        if (userInfo == null) {
            return Result.fail("账户不存在");
        }
        if(!userInfo.getPassword().equals(password)){
            return Result.fail("密码错误");
        }
        log.info(userInfo.toString());
        session.setAttribute(Constant.USER_SESSION_KEY,userInfo);
        return Result.success("");

    }
}
